import 'dart:convert';

import 'package:elephant_fish/TRTCLiveRoomDemo/model/TRTCLiveRoomDef.dart';
import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/live_join_request.dart';
import 'package:elephant_fish/model/live_join_entity.dart';
import 'package:elephant_fish/model/room_entity.dart';
import 'package:elephant_fish/page/login_page.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class VideoCard extends StatefulWidget {
  final RoomEntity roomEntity;
  const VideoCard({Key key, this.roomEntity}) : super(key: key);

  @override
  _VideoCardState createState() => _VideoCardState();
}

class _VideoCardState extends State<VideoCard> {
  LiveJoinEntity liveJoinEntity;
  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return InkWell(
        onTap: (){
          RoomInfo roomInfo = RoomInfo(roomId: widget.roomEntity.roomId, ownerId: widget.roomEntity.userId,roomName: widget.roomEntity.roomTitle);
          liveJoin(roomInfo);
        },
        child:SizedBox(
          child: Card(
            //取消卡片默认边距
            margin: EdgeInsets.all(3),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Stack(
                    children: [
                      Image.network(widget.roomEntity.cover == ''? 'https://img0.baidu.com/it/u=1820939238,2677153117&fm=26&fmt=auto&gp=0.jpg' :'http://119.28.131.94:82' + widget.roomEntity.cover,width:size.width / 2 - 6,height: size.width / 2 - 8 ,fit:BoxFit.cover,),
                      Align(
                        alignment: Alignment.topRight,
                        child: Padding(
                          padding: EdgeInsets.only(top: 5,right: 4),
                          child:
                          widget.roomEntity.status == '' ? Container(width: 0,height: 0,) :
                          Container(
                            width: 58.5,
                            height: 18,
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.all(Radius.circular(9)),
                              color: Color(0x20000000)
                            ),
                            child:Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                widget.roomEntity.status == '直播中' ?
                                Image.asset('assets/images/zhibo.png',width: 9,)
                                : widget.roomEntity.status == '连麦中' ? Image.asset('assets/images/lianmai.png',width: 9,)
                                :widget.roomEntity.status == 'PK中' ?  Image.asset('assets/images/pkzhong.png',width: 9,) : Container(width: 0,height: 0,),
                                SizedBox(width: 4.5,),
                                widget.roomEntity.status == '直播中' ?
                                Text('直播中',style: TextStyle(color: Color(0xffffffff),fontSize: 11 ),)
                                    : widget.roomEntity.status == '连麦中' ?  Text('连麦中',style: TextStyle(color: Color(0xffffffff),fontSize: 11 ),)
                                    :widget.roomEntity.status == 'PK中' ?   Text('PK中',style: TextStyle(color: Color(0xffffffff),fontSize: 11 ),) : Container(width: 0,height: 0,),

                              ],
                            ),
                          ),
                        ),
                      ),
                      Align(
                        alignment: Alignment.topLeft,
                        child: Padding(
                          padding: EdgeInsets.only(top: 140.5,left: 9.5),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(widget.roomEntity.nickname,style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                              SizedBox(height: 3.5,),
                              Row(
                                children: [
                                  widget.roomEntity.level ~/ 10 == 0 ?
                                  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/10_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) :  widget.roomEntity.level ~/ 10 == 1 ?
                                  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/20_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) :  widget.roomEntity.level ~/ 10 == 2 ?
                                  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/30_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) :  widget.roomEntity.level ~/ 10 == 3 ?
                                  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/40_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) :  widget.roomEntity.level ~/ 10 == 4 ?
                                  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/50_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) :  widget.roomEntity.level ~/ 10 == 5 ?
                                  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/60_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) :  widget.roomEntity.level ~/ 10 == 6 ?
                                  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/70_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) :  widget.roomEntity.level ~/ 10 == 7 ?
                                  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/80_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) :  widget.roomEntity.level ~/ 10 == 8 ?
                                  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/90_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) :  widget.roomEntity.level ~/ 10 == 9 ?
                                  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/100_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) :  widget.roomEntity.level ~/ 10 == 10 ?
                                  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/110_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) : widget.roomEntity.level ~/ 10 == 11 ?
                                  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/120_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) :  Container(
                                    width: 36.5,
                                    height: 12,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(2)),
                                      image: DecorationImage(
                                        image: AssetImage('assets/images/120_level.png'),
                                      ),
                                    ),
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Padding(
                                        padding: EdgeInsets.only(right: 4),
                                        child: Text(widget.roomEntity.level.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 11),),
                                      ),
                                    ),
                                  ) ,
                                  SizedBox(width: 3,),
                                  widget.roomEntity.title == '常在'
                                      ? Image.asset(
                                    'assets/images/changzaiicon.png',
                                    height: 16,
                                  )
                                      : widget.roomEntity.title == '良人'
                                      ? Image.asset(
                                    'assets/images/liangrenicon.png',
                                    height: 16,
                                  )
                                      : widget.roomEntity.title == '贵嫔'
                                      ? Image.asset(
                                    'assets/images/guipinicon.png',
                                    height: 16,
                                  )
                                      : widget.roomEntity.title ==
                                      '贵妃'
                                      ? Image.asset(
                                    'assets/images/guifeiicon.png',
                                    height: 16,
                                  )
                                      : widget.roomEntity.title ==
                                      '皇后'
                                      ? Image.asset(
                                    'assets/images/huanghouicon.png',
                                    height: 16,
                                  )
                                      : widget.roomEntity.title ==
                                      '神女'
                                      ? Image.asset(
                                    'assets/images/shennvicon.png',
                                    height: 16,
                                  )
                                      : widget.roomEntity.title ==
                                      '帝后'
                                      ? Image.asset(
                                    'assets/images/dihouicon.png',
                                    height: 16,
                                  )
                                      : widget.roomEntity.title ==
                                      '天君夫人'
                                      ? Image.asset(
                                    'assets/images/tianjunfurenicon.png',
                                    height: 16,
                                  )
                                      : Image.asset(
                                    'assets/images/nvwaicon.png',
                                    height: 16,
                                  )

                                ],
                              )
                            ],
                          ),
                        ),
                      ),
                      Align(
                        alignment: Alignment.topRight,
                        child: Padding(
                          padding: EdgeInsets.only(right: 10.5,top: 154),
                          child: Text(
                            widget.roomEntity.person != null ?  widget.roomEntity.person.toString() : '0',style:TextStyle(color: Color(0xfff3f3f3),fontSize: 11) ,
                          ),
                        ),
                      ),
                      Align(
                        alignment: Alignment.topLeft,
                        child: Padding(
                          padding: EdgeInsets.only(left: 8,top: 8),
                          child: Text(
                            '魅力榜',style:TextStyle(color: Color(0xfff3f3f3),fontSize: 11) ,
                          ),
                        ),
                      ),
                    ],
                  )

                ],
              ),
            ),
          ),
        ));
  }

  liveJoin(RoomInfo roomInfo) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    BaseRequest request = LiveJoinRequest();
    request.add('room_id', widget.roomEntity.roomId).addHeader('usertoken', appToken);
    var result = await HiNet.getInstance().fire(request);
    if (result['code'] == -1) {
      TxUtils.showErrorToast('登录失效，请重新登录', context);
      Navigator.pop(context);
      Navigator.of(context).push(MaterialPageRoute(
        builder: (context) {
          return LoginPage(arguments: {'loginCode':'-1'},);
        },
      ));
    }else if(result['code'] == 0){
      TxUtils.showToast('加入房间成功', context);
      liveJoinEntity = LiveJoinEntity.fromJson(new Map.from(result['data']));
      goRoomInfoPage(roomInfo);
    }
  }

  goRoomInfoPage(RoomInfo roomInfo) async {
    String loginUserId = await TxUtils.getLoginUserId();
    if (roomInfo.ownerId.toString() == loginUserId) {
      Navigator.pushReplacementNamed(
        context,
        "/liveRoom/roomAnchor",
        arguments: {
          "ownerId": roomInfo.ownerId,
          "roomName": roomInfo.roomName,
          'roomId': roomInfo.roomId,
          'isNeedCreateRoom': false,
        },
      );
      return;
    }
    Navigator.pushReplacementNamed(
      context,
      "/liveRoom/roomAudience",
      arguments: {
        'roomId': roomInfo.roomId,
        "ownerId": roomInfo.ownerId,
        "roomName": roomInfo.roomName,
        'isNeedCreateRoom': false,
      },
    );
  }
}
